package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.mall.domain.GoodsSpecifications;
import com.ruoyi.mall.domain.bo.GoodsSpecificationsBo;
import com.ruoyi.mall.domain.vo.GoodsSpecificationsVo;
import com.ruoyi.mall.mapper.GoodsSpecificationsMapper;
import com.ruoyi.mall.service.IGoodsSpecificationsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.GoodsSpecificationsDetailBo;
import com.ruoyi.mall.domain.vo.GoodsSpecificationsDetailVo;
import com.ruoyi.mall.domain.GoodsSpecificationsDetail;
import com.ruoyi.mall.mapper.GoodsSpecificationsDetailMapper;
import com.ruoyi.mall.service.IGoodsSpecificationsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品规格明细Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class GoodsSpecificationsDetailServiceImpl implements IGoodsSpecificationsDetailService {

    private final GoodsSpecificationsDetailMapper baseMapper;

    @Autowired
    private GoodsSpecificationsMapper goodsSpecificationsMapper;

    /**
     * 查询商品规格明细
     */
    @Override
    public GoodsSpecificationsDetailVo queryById(Long goodsSpecificationsDetailId){
        return baseMapper.selectVoById(goodsSpecificationsDetailId);
    }

    /**
     * 查询商品规格明细列表
     */
    @Override
    public TableDataInfo<GoodsSpecificationsDetailVo> queryPageList(GoodsSpecificationsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GoodsSpecificationsDetail> lqw = buildQueryWrapper(bo);
        LambdaQueryWrapper<GoodsSpecifications> lqws = Wrappers.lambdaQuery();
        List<GoodsSpecificationsVo> lstSpecificationsVoList = goodsSpecificationsMapper.selectVoList(lqws);
        Page<GoodsSpecificationsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (GoodsSpecificationsDetailVo goodsSpecificationsDetailVo :result.getRecords())
        {
            GoodsSpecificationsVo goodsSpecificationsVo = lstSpecificationsVoList.stream().filter(item -> item.getGoodsSpecificationsId().equals(goodsSpecificationsDetailVo.getGoodsSpecificationsId())).findFirst().orElse(null);
            if (goodsSpecificationsVo != null)
            {
                goodsSpecificationsDetailVo.setGoodsSpecificationsName(goodsSpecificationsVo.getName());
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品规格明细列表
     */
    @Override
    public List<GoodsSpecificationsDetailVo> queryList(GoodsSpecificationsDetailBo bo) {
        LambdaQueryWrapper<GoodsSpecificationsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GoodsSpecificationsDetail> buildQueryWrapper(GoodsSpecificationsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GoodsSpecificationsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsSpecificationsId() != null, GoodsSpecificationsDetail::getGoodsSpecificationsId, bo.getGoodsSpecificationsId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), GoodsSpecificationsDetail::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDesc()), GoodsSpecificationsDetail::getDesc, bo.getDesc());
        lqw.eq(bo.getYn() != null, GoodsSpecificationsDetail::getYn, bo.getYn());
        return lqw;
    }

    /**
     * 新增商品规格明细
     */
    @Override
    public Boolean insertByBo(GoodsSpecificationsDetailBo bo) {
        GoodsSpecificationsDetail add = BeanUtil.toBean(bo, GoodsSpecificationsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGoodsSpecificationsDetailId(add.getGoodsSpecificationsDetailId());
        }
        return flag;
    }

    /**
     * 修改商品规格明细
     */
    @Override
    public Boolean updateByBo(GoodsSpecificationsDetailBo bo) {
        GoodsSpecificationsDetail update = BeanUtil.toBean(bo, GoodsSpecificationsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GoodsSpecificationsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品规格明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
